import axiosIns, { axiosOdata } from './../utils/axios';
import axios from 'axios';
import update, { updateChain } from 'immutability-helper-x';
import { experienceData } from '../utils/mock';
/**
|--------------------------------------------------
| Constants
|--------------------------------------------------
*/
const GET_EXPERLIST = 'GET_EXPERLIST';
const GET_EXPERLISTING = 'GET_EXPERLISTING';
const GET_EXPERLISTFAIL = 'GET_EXPERLISTFAIL';
/**
|--------------------------------------------------
| Actions
|--------------------------------------------------
*/
export function experListIng() {
  return async (dispatch, getState) => {
    dispatch({
      type: GET_EXPERLISTING,
      loading: true
    });
  };
}
export function experListFail() {
  return async (dispatch, getState) => {
    dispatch({
      type: GET_EXPERLISTFAIL,
      loading: false
    });
  };
}

export function getExperList() {
  return async (dispatch, getState) => {
    const { experience, user } = getState();
    if (experience.loading) return;
    dispatch(experListIng());
    // const { token } = user.userInfo;
    const response = await axiosIns('/getExperList');
    const { data, status } = response;
    if (data && status == 200) {
      dispatch({
        type: GET_EXPERLIST,
        payLoad: data.list,
        loading: false
      });
    } else {
      dispatch(experListFail());
    }
  };
}

/**
|--------------------------------------------------
| Action Handlers
|--------------------------------------------------
*/
const ACTION_HANDLERS = {
  [GET_EXPERLISTING]: (state, action) => {
    // return ({ ...state, fetching: true })
    return update.$set(state, 'loading', action.loading);
  },
  [GET_EXPERLISTFAIL]: (state, action) => {
    // return ({ ...state, fetching: true })
    return update.$set(state, 'loading', action.loading);
  },
  [GET_EXPERLIST]: (state, action) => {
    return { ...state, loading: action.loading, list: action.payLoad };
    // return update.$set(state, 'loading', action.payLoad);
  }
};

/**
|--------------------------------------------------
| Reducer
|--------------------------------------------------
*/
const initialState = {
  loading: false,
  hasMore: true,
  list: null,
  pageSize: 10,
  pageNum: 0,
  keyword: '',
  detail: {
    loading: false,
    content: null
  }
};

export default function experienceReducer(state = initialState, action) {
  const handler = ACTION_HANDLERS[action.type];
  return handler ? handler(state, action) : state;
}
